package com.ustc.py.utils;


import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

@Component
public class CodeUtil {
    final Base64.Decoder decoder = Base64.getDecoder();
    final Base64.Encoder encoder = Base64.getEncoder();

    public String encode(String code) throws UnsupportedEncodingException {
        if(code==null||code.isEmpty()) return null;
        byte[] textByte = code.getBytes("UTF-8");
        return encoder.encodeToString(textByte);
    }

    public String decode(String encodeText) throws UnsupportedEncodingException {
        return new String(decoder.decode(encodeText), "UTF-8");
    }
}
